import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/Index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: Index,
      meta:{
        title:"首页"
      },
      children:[
        {
          path: '/books',
          name: 'books',
          component: () => import('../components/Books.vue'),
          meta:{
            title:"图书页"
          },
        },
    {
      path: '/users',
      name: 'users',
      component: () => import('../components/Users.vue'),
      meta:{
        title:"用户页"
      },
    },
    {
      path: '/cars',
      name: 'Cars',
      component: () => import('../components/Cars.vue'),
      meta:{
        title:"购物车页面"
      },
    },
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
      meta:{
        title:"登录页"
      },
    },
    
  ],
})


//添加路由守卫（前置守卫）
// import {ElMessage} from 'element-plus'
router.beforeEach((to,from,next)=>{

  if(to.path ==='/login'){
    next()//放行
  }else if(localStorage.getItem("token")){
    next()
  }else{
    ElMessage.warning("请重新登录")
    next({path:"/login"})
  }

  
})


//添加路由守卫（后卫守卫）
router.afterEach((to,from,next)=>{
  document.title=to.meta.title
})


export default router



